package com.gisuni.sddc.platformManager.service;

import org.nutz.log.Log;
import org.nutz.log.Logs;

import java.util.Iterator;

import javax.servlet.http.HttpServletRequest;

import org.json.JSONArray;
import org.json.JSONObject;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;

import com.gisuni.sddc.platformManager.dao.LayerTableNameDao;
import com.gisuni.sddc.platformManager.dao.ProjectDetailDao;
import com.gisuni.sddc.platformManager.model.Response;
import com.gisuni.sddc.util.StringUtil;

@IocBean
public class ProjectDetailService   {

	private Log log = Logs.getLog(ProjectDetailService.class);    
    @Inject
    private ProjectDetailDao projectDetailDao;
	@Inject
    private LayerTableNameDao layerTableNameDao;
	
	/**
	 * 获取字段信息[字段名:类型:注释]
	 * @param table
	 * @return
	 */
	public Object getColumnInfo(String table) {
		if(StringUtil.isNullOrEmpty(table)){
			return null;
		}
		/**
		 * 
 		select distinct(t.column_name||':'||t1.data_type||':'||t.comments) from all_col_comments t left join user_tab_columns t1 
 		on t.table_name = t1.TABLE_NAME and t.column_name = t1.COLUMN_NAME where t.table_name = 'ZCQXM_DETAIL';
 
		 */
		String sql = " select  t.column_name||':'||t1.data_type||':'||t.comments  result from all_col_comments t left join user_tab_columns t1 "
					+ " on t.table_name = t1.TABLE_NAME and t.column_name = t1.COLUMN_NAME where t.table_name = '"+table+"'";
		String result = null;
		
		try{
			result = projectDetailDao.querySql(sql);
		}catch(Exception e){
			log.error(e.toString());
			throw e;
		}
		return result;
	}


	/**
	 * 获取项目数据
	 * @param table
	 * @param pkUUid
	 * @return
	 */
	public Response getProjectDetailData(String table , String pkUUid) {
		Response response = new Response();
		response.setSuccess(false);
		
		String sql = "select * from "+table+" t where t.pkuuid = '"+pkUUid+"'";
		String result = null ; 
		try{
			result = projectDetailDao.querySql(sql);
			response.setSuccess(true);
			response.setResult(result);
		}catch(Exception e){
			log.error(e.toString());
			throw e;
		}
		return response;
	}

	/**
	 * 编辑项目数据
	 * @param request
	 * @return
	 */
	public Response editProject(HttpServletRequest request) {
		
		Response response = new Response();
		response.setSuccess(false);
		
		 String pkuuid = request.getParameter("PKUUID");
		 String tableName = request.getParameter("tableName");
		 
		//查询明细表是否有pkuuid 无则创建一条带有pkuuid的数据[可直接增加!!]
		 String checkSQL = "select * from "+tableName+" where PKUUID ='"+pkuuid+"'";
		 JSONObject checkObj = projectDetailDao.querySqlReturnJSONObj(checkSQL);
		 if(checkObj.length()==0){
			 projectDetailDao.executeSQL("insert into "+tableName+"(pkuuid) values('"+pkuuid+"') ");
		 }
		 
		//数字/日期的校验 TODO
	
		 String column = (String) getColumnInfo(tableName);
		 String sql  = getEditSQL(column,request);
		 
		 try{
			 Boolean flag = projectDetailDao.executeSQL(sql);
			 
		 }catch(Exception e ){
			 response.setMsg("执行sql操作失败"+e.toString());
			 log.error(e);
		 }
		 
		return response;
	}


	/**
	 * 生成编辑的sql
	 * @param column
	 * @param request
	 * @return
	 */
	private String getEditSQL(String column, HttpServletRequest request) {
		 String tableName = request.getParameter("tableName");
		 String pkuuid = request.getParameter("PKUUID");
		
		StringBuffer sbf = new StringBuffer();
		if(!StringUtil.isNullOrEmpty(column)){
			JSONArray arr = new JSONArray(column);
			for(int i =0 ;i<arr.length();i++){
				JSONObject obj  = (JSONObject) arr.get(i);
				
				String result = (String) obj.get("RESULT");
				String columnName = result.split(":")[0]; //字段名称
				String columnType = result.split(":")[1];  //字段类型
				
				 String columnValue = request.getParameter(columnName);
				 if(!StringUtil.isNullOrEmpty(columnValue)){
					 if("NUMBER".equals(columnType)){
						 sbf.append(columnName+"="+columnValue+" , ");
					 }else if("VARCHAR2".equals(columnType)){
						 sbf.append(columnName+"='"+columnValue+"', ");
					 }
					 
				 }
				 
			}
			
		}
		String sql = "update "+tableName +" set ";
		sql+=(sbf.toString().substring(0,sbf.toString().length()-2)+" where pkuuid ='"+pkuuid+"'");
		System.out.println(sql);
		return sql;
	}


	public Response deleteProject(HttpServletRequest request) {
		 Response response = new Response();
		 response.setSuccess(false);
		 String tableName = request.getParameter("tableName");
		 String pkuuid = request.getParameter("PKUUID");
		 
		 if(StringUtil.isNullOrEmpty(tableName) || StringUtil.isNullOrEmpty(pkuuid)){
			 response.setMsg("表名或pkuuid为空,删除失败");
			 return response;
		 }
		 
		 String sql = "delete from "+tableName+" where pkuuid = '"+pkuuid+"'";
		 try{
			 projectDetailDao.executeSQL(sql);
		 }catch(Exception e){
			 log.error(e);
			 response.setMsg("sql有误");
			 return response;
		 }
		 response.setSuccess(true);
		 return response;
	} 
	
	
}
